import numpy as np
import cv2 as cv

# 读取图像
image_path = "../roi_img.jpg"
img = cv.imread(image_path)

# 检查图像是否成功读取
if img is None:
    print(f"Error: Image not found or unable to open the image at path: {image_path}")
    exit(1)

# 改变颜色空间  BGR -> Gray
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 改变颜色空间  BGR -> HSV
img_hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)



# cv.imshow("LingXiao", img)
# cv.imshow("gray", img_gray)
# cv.imshow("hsv", img_hsv)

cap = cv.VideoCapture("../roi.mp4")
while(1):
    # Take each frame
    _, frame = cap.read()
    # Convert BGR to HSV
    hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
    # define range of blue color in HSV
    lower_blue = np.array([90,22,50])
    upper_blue = np.array([130,100,100])
    # Threshold the HSV image to get only blue colors
    mask = cv.inRange(hsv, lower_blue, upper_blue)
    # Bitwise-AND mask and original image
    res = cv.bitwise_and(frame,frame, mask= mask)
    cv.imshow('frame',frame)
    cv.imshow('mask',mask)
    cv.imshow('res',res)
    # 等待按键
    print("Press ESC to close the windows.")

    key = cv.waitKey(0)
    if key == 27:  # ESC 键
        break
    else:
        print(f"Key pressed: {key}")

# 关闭所有窗口
cv.destroyAllWindows()


